<template>
  <div class="all">
    <div class="top">
      <header>
        <div class="div">
          <img src="../../../assets/返回.png" @click="back()" alt="">
          <span class="solid" @click="back()">返回</span>
        </div>
        <div class="div">
          <span>服务消息</span>
        </div>
        <div class="div">
          <span class="solid"></span>
        </div>
      </header>
      <van-search
        v-model="searchValue"
        shape="round"
        background="#fff"
        @keyup="getSearch()"
        placeholder="请输入搜索关键词"
      />
    </div>
    <div class="box" @click="clickSys(item)" v-for="(item, index) in list" :key="index">
      <div class="box_left">
        <span class="year">{{getYear(item.created_at)}}</span>
        <span class="date">{{getMouth(item.created_at)}}</span>
      </div>
      <div class="box_right">
        <p class="jieshao" style="display:flex;justify-content: space-between">
          <span>
            {{item.name}}医生
          <van-badge dot v-show="Number(item.is_read) === 0"></van-badge>
          </span>
          <span class="time">
            {{formatDate(item.time)}}
          </span>
        </p>
        <p class="text">服务包内容：{{item.content}}</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ServiceMessage',
  data () {
    return {
      searchValue: '',
      list: []
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    // 获取列表信息
    getList () {
      this.$http.findAllServiceMsg({name: this.searchValue})
        .then(res => {
          console.log(res)
          if (res.data.status === 'success') {
            this.list = res.data.data
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 搜索
    getSearch () {
      this.getList()
    },
    // 点击某一个服务消息 红点消失
    clickSys (item) {
      this.$http.findOneServiceMsg({id: item.id})
        .then(res => {
          console.log(res)
          if (res.data.status === 'success') {
            item.is_read = 1
            //  /serviceDetails
            this.$router.push({path: '/keepServicePack',
              query: {
                id: item.package_id,
                isEdit: 0
              }})
            sessionStorage.setItem('serviceMessage', JSON.stringify(item))
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    getYear (data) {
      return data.toString().split('-')[0]
    },
    getMouth (data) {
      return data.toString().split('-')[1] + '-' + data.toString().split('-')[2]
    },
    formatDate (date) {
      var date = new Date(date)
      var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
      var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
      var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
      return hh + mm + ss
    },
    // 返回
    back () {
      this.$router.history.go(-1)
    }
  }
}
</script>

<style scoped>
  .all{
    width: 100%;
    height: 100%;
    background: #FAF9FE;
    overflow: auto;
  }
  .top{
    position: sticky;
    top: 0;
    z-index: 9;
  }
  header{
    width: 100%;
    height: 44px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #E5E5E5;
    background: white;
  }
  .div{
    width: 33%;
    height: 100%;
    display: flex;
    align-items: center;
    font-family: "PingFang SC";
    font-size: 15px;
    font-weight: bold;
  }
  .div img{
    margin-right: 5px;
  }
  .div:nth-child(1) img{
    height: 13px;
    width: 7px;
    margin-left: 15px;
  }
  .div:nth-child(3) img{
    margin-right: 5px;
  }
  .div:nth-child(3) span{
    margin-right: 15px;
  }
  .div:nth-child(2){
    justify-content: center;
  }
  .div:nth-child(3){
    justify-content: flex-end;
  }
  .solid{
    color: #1DA4F2;
  }
  /*/deep/ .van-search{*/
  /*  padding: 0 12px;*/
  /*  height: 44px;*/
  /*}*/
  .box{
    width: 95%;
    padding: 10px;
    box-sizing: border-box;
    box-shadow: 0px 5px 20px 0px rgba(0, 0, 0, 0.05);
    border-radius: 10px;
    margin: 10px auto auto auto;
    display: flex;
    background: white;
  }
  .box:last-child{
    margin-bottom: 30px;
  }
  .box_left{
    display: flex;
    flex-direction: column;
  }
  .year{
    font-size: 15px;
    font-family: PingFang SC;
    font-weight: bold;
    color: #333333;
  }
  .date{
    color: #666;
    font-size: 11px;
  }
  .box_right{
    padding: 0 0 0 8px;
    box-sizing: border-box;
    width: 80%;
  }
  .box_right p{
    /*//多行*/
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;  /*/显示几行/*/
    overflow: hidden;
    line-height: 1.7;
  }
  .jieshao{
    font-size: 15px;
    font-family: PingFang SC;
    font-weight: 500;
    color: #333333;
  }
  .time{
    font-size: 13px;
    font-family: "PingFang SC";
    font-weight: 400;
  }
  .text{
    font-size: 13px;
    font-family: PingFang SC;
    font-weight: 500;
    color: #999999;
  }
</style>
